package hust.rpc.common;

import lombok.Data;

import java.io.Serializable;
import java.net.InetSocketAddress;

/**
 * 增加 Address 类，用来表示服务的地址
 * 包括ip地址和端口号
 */
@Data
public class Address implements Serializable {

    private String host;

    private int port;

    public Address() {
    }

    public Address(String host, int port) {
        this.host = host;
        this.port = port;
    }

    /**
     * 使用 set 时，必须重写 hashcode 和 equal，
     * 需确保两个对象 equal 相等时，hashcode 也必须相等
     * @return
     */
    @Override
    public int hashCode() {
        return host.hashCode() + port;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof Address){

            Address a = (Address) obj;

            return host.equals(a.getHost()) && this.port == a.getPort();
        }
        return false;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

}
